package com.agileboot.attendance.domain.acceptance.model;

import com.agileboot.attendance.domain.acceptance.dto.AcceptanceQuery;
import com.agileboot.attendance.domain.acceptance.entity.AttAcceptanceEntity;
import com.agileboot.attendance.domain.acceptance.db.service.AttAcceptanceService;
import com.agileboot.common.core.page.PageDTO;
import com.agileboot.common.exception.ApiException;
import com.agileboot.common.exception.error.ErrorCode;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 验收单模型工厂
 */
@Component
@RequiredArgsConstructor
public class AcceptanceModelFactory {

    private final AttAcceptanceService acceptanceService;

    /**
     * 创建新的验收单模型
     */
    public AcceptanceModel create() {
        return new AcceptanceModel(new AttAcceptanceEntity(), acceptanceService);
    }

    /**
     * 根据ID加载验收单模型
     */
    public AcceptanceModel loadById(Long acceptanceId) {
        AttAcceptanceEntity entity = acceptanceService.getById(acceptanceId);
        if (entity == null) {
            throw new ApiException(ErrorCode.Business.COMMON_OBJECT_NOT_FOUND, "验收单", acceptanceId);
        }
        return new AcceptanceModel(entity, acceptanceService);
    }

    /**
     * 分页查询验收单
     */
    public PageDTO<AcceptanceModel> selectPage(AcceptanceQuery query) {
        Page<AttAcceptanceEntity> page = acceptanceService.selectPage(query);
        List<AcceptanceModel> records = page.getRecords().stream()
            .map(entity -> new AcceptanceModel(entity, acceptanceService))
            .collect(Collectors.toList());
        Long total = page.getTotal();
        return new PageDTO<>(records, total);
    }
} 